import { NameSpace } from 'utils/index';
import exceed from 'utils/apimap';
import { getFeature } from 'utils/features';

const ns = NameSpace('repo-settings');
export const UPDATE_DATA = ns('UPDATE_DATA');

export function updateData(data) {
  return (dispatch) => {
    dispatch({
      type: UPDATE_DATA,
      data,
    });
  };
}

export function checkDeployKeys() {
  return (dispatch) => {
    exceed
      .forceFetch({
        api: 'project.check_deploy_keys',
      })
      .then((result) => {
        const { has_deploy_keys } = result;
        updateData({
          has_deploy_keys,
        })(dispatch);
      });
  };
}

export function getLfsInfo() {
  return (dispatch) => {
    const { pathResource } = window.FORCE;
    exceed
      .fetch({
        api: 'project.lfs.info.get',
        params: {
          projectId: pathResource.id,
        },
      })
      .then((result) => {
        updateData({
          lfsInfo: result,
        })(dispatch);
      });
  };
}

export function getIsResourceUnLimit() {
  return (dispatch) => {
    const { pathResource } = window.FORCE;
    if (!getFeature('project.resource.limit.privilege')) {
      return;
    }
    exceed
      .fetch({
        api: 'project.resource.limit.privilege',
        params: {
          projectId: pathResource.id,
        },
      })
      .then((result) => {
        updateData({
          lfsUnlimit: result,
        })(dispatch);
      });
  };
}
